package com.homework.ch01;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/11/5
 * @Description:
 */
public class Test01 {
    //连接数据库的四要素
    private static String driverClass="com.mysql.cj.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/briup?characterEncoding=utf8&useUnicode=true";
    private static String username="briup";
    private static String password="briup";

    public static void main(String[] args) {
        //资源对象
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        //建立连接
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        try {
            Class.forName(driverClass);
            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();

            //插入数据
            String sql1 = "insert into t_customer(name,age,birthday) values('zs',18,'1999-01-01')";
            String sql2 = "insert into t_customer(name,age,birthday) values('ls',20,'1997-05-05')";
            String sql3 = "insert into t_customer(name,age,birthday) values('ww',19,'2003-08-08')";
            stmt.executeUpdate(sql1);
            System.out.println(sql1);
            stmt.executeUpdate(sql2);
            System.out.println(sql2);
            stmt.executeUpdate(sql3);
            System.out.println(sql3);

            //查询数据,并转化
            List<Customer> list = new ArrayList<>();
            String sql = "SELECT id, name, age, birthday FROM t_customer WHERE birthday < '2000-01-01'";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                Customer c = new Customer();
                c.setId(rs.getInt("id"));
                c.setName(rs.getString("name"));
                c.setAge(rs.getInt("age"));
                c.setBirthday(rs.getDate("birthday"));
                list.add(c);
            }


            //序列化
            oos = new ObjectOutputStream(new FileOutputStream("3-jdbc-demo/src/list.txt"));
            oos.writeObject(list);
            ois = new ObjectInputStream(new FileInputStream("3-jdbc-demo/src/list.txt"));
            List<Customer> list2 = (List<Customer>) ois.readObject();
            for (Customer c : list2) {
                System.out.println(c);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            //释放资源
            try {
                if (ois != null) {
                    ois.close();
                }
                if (oos != null) {
                    oos.close();
                }
                if (rs != null) {
                    rs.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}
